package com.banksoft.cps.controller.company;

import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.PoliceCheck;
import com.banksoft.cps.bean.query.PoliceCheckQuery;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.enums.CheckProcess;
import com.banksoft.cps.service.PoliceCheckService;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/11/19.
 */
@Controller
@RequestMapping("/company/policeCheck")
public class CPoliceCheckController extends ControllerConstants {
    @Autowired
    PoliceCheckService policeCheckService;
    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(PoliceCheckQuery query){
        Company company = (Company) UserContext.getUser();
        query.setCompanyId(company.getId());
        query.setNoProcess(CheckProcess.create.name());
        List<PoliceCheck> list =  policeCheckService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(PoliceCheckQuery query){
        Company company = (Company) UserContext.getUser();
        query.setCompanyId(company.getId());
        long count =  policeCheckService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }

    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        PoliceCheck bean = policeCheckService.find(id);
        return toJSONString(createDataTip(bean));
    }

    @RequestMapping(value = "audit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String audit(PoliceCheck bean, HttpServletRequest request){
        try {
            Company company = (Company) UserContext.getUser();
            policeCheckService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
}
